package com.example.smartall.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.LocationBean;
import com.example.smartall.ui.base.BaseActivity;
import com.google.gson.Gson;

import java.util.HashMap;

/**
  * @Author:         abbas
  * @CreateDate:     2022/1/31
  * @Description:    缴费主页：
  *                  首页定位左侧可自动定位到所在城市，右侧显示返回上一页面按钮。
  *                  本页面有水费、电费、户号管理和相关资讯等模块，底部栏设置自动缴费|户号管理。
  */
public class PaymentActivity extends BaseActivity implements View.OnClickListener{

    private TextView tvLocation;
    private Button btBack;
    private TextView tvElectric;
    private TextView tvWater;
    private ListView lvNews;
    private TextView tvAutoCharge;
    private TextView tvAccountSet;

    private HashMap<Integer,Class> intentParamsMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);
        //跳转数据组装
        getIntentParamsMap();

        init();
        initEvent();
        getLocation();
    }

    @Override
    public void init() {
        tvLocation = (TextView) findViewById(R.id.tv_location);
        btBack = (Button) findViewById(R.id.bt_back);
        tvElectric = (TextView) findViewById(R.id.tv_electric);
        tvWater = (TextView) findViewById(R.id.tv_water);
        lvNews = (ListView) findViewById(R.id.lv_news);
        tvAutoCharge = (TextView) findViewById(R.id.tv_autoCharge);
        tvAccountSet = (TextView) findViewById(R.id.tv_accountSet);

    }

    @Override
    public void initEvent() {
        btBack.setOnClickListener(this);
        tvElectric.setOnClickListener(this);
        tvWater.setOnClickListener(this);
        tvAutoCharge.setOnClickListener(this);
        tvAccountSet.setOnClickListener(this);
    }

    private void getIntentParamsMap(){
        intentParamsMap = new HashMap<>();
        intentParamsMap.put(R.id.tv_electric,PElectricActivity.class);
        intentParamsMap.put(R.id.tv_water,PWaterActivity.class);
        intentParamsMap.put(R.id.tv_autoCharge,HomeActivity.class);
        intentParamsMap.put(R.id.tv_accountSet,PAccountSetActivity.class);

    }

    //当前位置
    private void getLocation(){
        Api.config(ApiConfig.IN_LOCATION,null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                LocationBean locationBean = new Gson().fromJson(res,LocationBean.class);
                if (locationBean.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvLocation.append(locationBean.getData().getCity());
                        }
                    });
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        if (intentParamsMap != null){
            if (intentParamsMap.containsKey(view.getId())){
                migrateTo(intentParamsMap.get(view.getId()));
            }else{
                PaymentActivity.this.finish();
            }
        }
    }
}